﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Reflexive.IO
{
    public class Convertible<TConverter, TSource, TDestination> : IComparable<TDestination>
        where TConverter :  ITypeConverter<TSource, TDestination>, new()
        where TDestination : class
    {
        TSource _internalItem;
    
        private Convertible(TSource item)
        {
            _internalItem = item;
        }

        public static implicit operator TSource(Convertible<TConverter, TSource, TDestination> item)
        {
            return item._internalItem;
        }

        public static implicit operator TDestination(Convertible<TConverter, TSource, TDestination> item)
        {
            return Activator.CreateInstance<TConverter>().Convert(item._internalItem);
        }

        public static implicit operator Convertible<TConverter, TSource, TDestination>(TSource item)
        {
            return new Convertible<TConverter, TSource, TDestination>(item);
        }

        public static Convertible<TConverter, TSource, TDestination> Create(TSource item)
        { 
            return item;
        }

        #region overrides
        public int CompareTo(TDestination other)
        {
            var t = (IComparable<TDestination>)Activator.CreateInstance<TConverter>().Convert(_internalItem);
           return t.CompareTo(other);
        }

        public override int GetHashCode()
        {
            return _internalItem.GetHashCode();
        }

        public override string ToString()
        {
            return _internalItem.ToString();
        }

        public override bool Equals(object obj)
        {
            return _internalItem.Equals(obj);
        }

        #endregion
    }
}
